package cn.ffcs.uec.outStaff.dao.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import cn.ffcs.uec.common.util.PermissionUtil;
import cn.ffcs.uec.outStaff.dao.UecOutStaffContractDao;
import cn.ffcs.uec.outStaff.model.UecOutStaffContract;
import cn.ffcs.uec.outStaff.model.UecOutStaffInfo;
import cn.ffcs.uec.outStaff.vo.UecOutStaffContractVo;
import cn.ffcs.uom.common.constants.BaseUnitConstants;
import cn.ffcs.uom.common.dao.BaseDaoImpl;
import cn.ffcs.uom.common.util.StrUtil;
import cn.ffcs.uom.common.vo.PageInfo;

@Repository("uecOutStaffContractDao")
@Transactional
public class UecOutStaffContractDaoImpl extends BaseDaoImpl implements UecOutStaffContractDao {

	@Override
	public PageInfo queryPageInfoByUecOutStaffContract(UecOutStaffInfo uecOutStaffInfo, int current, int pageSize) {
		if (null != uecOutStaffInfo) {
			StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSN_PERCON T WHERE 1 = 1 ");
			List<Object> params = new ArrayList<Object>();
			sb.append(" AND T.STATUS_CD = ? ");
			params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);

			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPkPsnMsg())) {// 判断数据是否是集团的
				sb.append(" AND T.PK_PSN_MSG = ? ");
				params.add(uecOutStaffInfo.getPkPsnMsg());
			} else if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getOutStaffId())) {
				sb.append(" AND T.OUT_STAFF_ID = ? ");
				params.add(uecOutStaffInfo.getOutStaffId());
			}
			return this.jdbcFindPageInfo(sb.toString(), params, current, pageSize, UecOutStaffContract.class);
		}
		return null;
	}

	@Override
	public PageInfo queryPageInfo(int current, int pageSize) {
		String sqlStr = "SELECT u.psncode,u.psnname,u.idnum,u.mobtelephone,t.pk_termtype,"
				+ "t.ct_begin_date,t.ct_end_date,t.trial_period,t.actual_end_date,"
				+ "t.ct_change_type,t.workhour_system,t.ct_major "
				+ "FROM UEC_OUT_STAFF_INFO U, UEC_PSN_PERCON T, UEC_ORG_ORGS S"
				+ " WHERE T.STATUS_CD = '1000' AND U.STATUS_CD = '1000' "
				+ " AND T.PK_PSN_MSG = U.PK_PSN_MSG AND U.PK_ORG = S.PK_ORG "
				+ "AND t.ct_end_date < to_char(trunc(SYSDATE + 30),'yyyy-mm-dd') " + "AND S.Name LIKE ? ";
		String org = PermissionUtil.getManagerOrg();
		List<Object> params = new ArrayList<Object>();
		params.add("%" + org + "%");

		return this.jdbcFindPageInfo(sqlStr, params, current, pageSize, UecOutStaffContractVo.class);
	}

	@Override
	public UecOutStaffContract qryUecOutStaffContractByUecOutStaffInfo(UecOutStaffInfo uecOutStaffInfo) {
		StringBuilder sb = new StringBuilder(" SELECT * FROM UEC_PSN_PERCON T WHERE T.STATUS_CD = ? AND T.NEW_MARK = 'Y' ");
		List<Object> params = new ArrayList<Object>();
		params.add(BaseUnitConstants.ENTT_STATE_ACTIVE);
		if (null != uecOutStaffInfo) {
			if (!StrUtil.isNullOrEmpty(uecOutStaffInfo.getPkPsnMsg())) {
				sb.append(" AND T.PK_PSN_MSG = ? ");
				params.add(uecOutStaffInfo.getPkPsnMsg());
			}
			List<UecOutStaffContract> list = super.jdbcFindList(sb.toString(), params, UecOutStaffContract.class);
			if (list.size() > 0 && list != null) {
				return list.get(0);
			}
		}
		return null;
	}
}
